package com.imooc.course.generator.server;

import com.imooc.course.generator.util.DbUtil;
import com.imooc.course.generator.util.Field;
import com.imooc.course.generator.util.FreemarkerUtil;
import com.imooc.course.generator.util.ReadGeneratorConfigXml;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.util.*;

public class ServerGenerator {

    //占位符//同时也是模块名
    private static String model = "server";
    //生成的文件在这里
    private static String toPath = model+"\\src\\main\\java\\com\\imooc\\course\\"+model;
    //generatorConfig.xml文件的位置的路径
    private static String generatorConfigPath = "server\\src\\main\\resources\\generator\\generatorConfig.xml";

    //占位符//同时也是表名，service类的前缀
    private static String Domain;
    private static String domain;
    private static List<Field> fieldList;
    private static Set<String> typeSet;

    public static void main(String[] args) throws Exception {

        init(generatorConfigPath);

        Map map = getMap();

        dto(map);
        service(map);
        serviceImpl(map);
    }



    private static void dto(Map map) throws IOException, TemplateException {
        //配置文件位置
        String path = "server\\dto\\";
        //使用哪个配置文件
        String ftl = "dto.ftl";
        //DTO生成目标路径
        String toDtoPath = toPath+"\\dto\\";

        server(path,ftl,toDtoPath + Domain + "Dto.java", map);
    }

    private static void service(Map map) throws IOException, TemplateException {
        //配置文件位置
        String path = "server\\service\\";
        //使用哪个配置文件
        String ftl = "service.ftl";
        //生成service.java的终点位置路径段
        String targetPath = "\\service\\";

        server(path,ftl,toPath+targetPath+Domain+"Service.java",map);
    }

    private static void serviceImpl(Map map) throws IOException, TemplateException {
        //配置文件位置
        String path = "server\\service\\";
        //使用哪个配置文件
        String ftl = "serviceImpl.ftl";
        //生成文件的终点位置路径段
        String targetPath = "\\service\\";

        server(path,ftl,toPath+targetPath+"impl\\"+Domain+"ServiceImpl.java",map);

    }

    private static Map getMap() throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("Domain", Domain);
        map.put("domain", domain);
        map.put("model", model);
        map.put("fieldList", fieldList);
        map.put("typeSet", typeSet);
        return map;
    }

    private static void init(String generatorConfigPath) throws Exception {
        ReadGeneratorConfigXml init = ReadGeneratorConfigXml.init(generatorConfigPath);
        Domain = init.getDomainObjectName();
        domain = init.getLowerCaseDomain();
        //生成dto需要准备的变量
        fieldList = DbUtil.getColumnByTableName(init.getTableName());
        typeSet = DbUtil.getJavaTypes(fieldList);
    }

    private static void server(String path,String config,String fileName,Map map) throws IOException, TemplateException {

        FreemarkerUtil.init(path)
                .config(config)
                .generator(fileName,map);

    }


}
